import logging

def get_logger(name, logfile=None, level='DEBGU'):
    lmap = {'DEBUG': logging.DEBUG,
            'INFO': logging.INFO,
            'WARN': logging.WARN,
            'ERROR': logging.ERROR,
            'CRITICAL': logging.CRITICAL}
    fmt = ("[%(asctime)s (%(module)s):%(lineno)3d) ] %(levelname)5s - "
           "%(message)s")

    datefmt = "%Y-%m-%d %H:%M"

    user_log_level = lmap.get(level.upper(), logging.DEBUG)
    logger = logging.getLogger(name)
    logger.setLevel(user_log_level)

    formatter = logging.Formatter(fmt, datefmt)

    ch = logging.StreamHandler()
    ch.setFormatter(formatter)
    ch.setLevel(logging.DEBUG)
    logger.addHandler(ch)

    if logfile is not None:
        fh = logging.FileHandler('debug.log')
        fh.setFormatter(formatter)
        fh.setLevel(user_log_level)
        logger.addHandler(fh)

    return logger


def test():
    logname = 'test-log-module'
    logfile = 'test-log.log'
    loglevel = 'debug'
    logger = get_logger(logname, logfile, loglevel)
    logger.debug('debug')
    logger.info('info')
    logger.warn('warn')
    logger.error('error')
    logger.critical('critical')

if __name__ == "__main__":
    test()
